package cn.yinyu.queue.module.ecg.controller.admin.call;

import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.call.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.call.CallDO;
import cn.yinyu.queue.module.ecg.service.call.CallService;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 叫号")
@RestController
@RequestMapping("/ecg/call")
@Validated
public class CallController {

    @Resource
    private CallService callService;

    @PostMapping("/create")
    @Operation(summary = "创建叫号")
    @PreAuthorize("@ss.hasPermission('ecg:call:create')")
    public CommonResult<Integer> createCall(@Valid @RequestBody CallSaveReqVO createReqVO) {
        return success(callService.createCall(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新叫号")
    //@PreAuthorize("@ss.hasPermission('ecg:call:update')")
    @PermitAll
    public CommonResult<Boolean> updateCall(@Valid @RequestBody CallSaveReqVO updateReqVO) {
        callService.updateCall(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除叫号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:call:delete')")
    public CommonResult<Boolean> deleteCall(@RequestParam("id") Integer id) {
        callService.deleteCall(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得叫号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:call:query')")
    public CommonResult<CallRespVO> getCall(@RequestParam("id") Integer id) {
        CallDO call = callService.getCall(id);
        return success(BeanUtils.toBean(call, CallRespVO.class));
    }

    @GetMapping("/next")
    @Operation(summary = "大屏..获得下一个叫号")
    @PermitAll
    public CommonResult<CallRespVO> nextCall() {
        CallDO call = callService.getNextCall();
        return success(BeanUtils.toBean(call, CallRespVO.class));
    }

    @GetMapping("/next-install")
    @Operation(summary = "诊间屏..获得下一个叫号")
    @PermitAll
    public CommonResult<CallRespVO> nextInstallCall(HttpServletRequest request) {
        String reqIp = ServletUtil.getClientIP(request);

        CallDO call = callService.getNextInstallCall(reqIp);
        return success(BeanUtils.toBean(call, CallRespVO.class));
    }

}

